<?php
    /**
     * BrowseBehavior 
     * 浏览行为
     * @uses Behavior
     * @package 
     * @version $id$
     * @copyright 2009-2011 SamPeng 
     * @author SamPeng <sampeng87@gmail.com> 
     * @license PHP Version 5.2 {@link www.sampeng.cn}
     */
    class BrowseService extends Service{
        private static $uid  = 0;
        private static $id  = 0;
        private static $type;
        private static $lefttime = 0;
        private $key = "dig";
        private $_cacheMode;
        private $_old_cache;

        /**
         * cache 
         * 浏览的缓存
         * @var array
         * @access private
         */
        private $_cache = array();

        public function __construct($options = null){
            $this->setOptions( $options );
            $this->_cacheMode = service('Xdata');
            $this->_old_cache = $this->_cacheMode->get( 'browse:'.$this->key );
            $this->_cache = $this->_old_cache[self::$type];
        }

        public function run(){
        	$time = time();
        	switch (true){
        		case is_bool($this->_old_cache):
					        			 $this->_old_cache = array();
					        			 $this->_cache['endTime'] = $time;
						                 $this->_cache[self::$id][self::$uid] = 1;
						                 $this->_setCache(false);
						                 return 1;
        		case empty($this->_cache) || empty($this->_old_cache):
        								 $this->_cache['endTime'] = $time;
					        			 $this->_cache[self::$id][self::$uid] = 1;
						            	 $this->_setCache(true);
						            	 return 1;
        	}
        	if( isset($this->_cache[self::$id][self::$uid]) && self::$lefttime > $time-$this->_cache['endTime']) return 0;
        	
        	if( !isset($this->_cache[self::$id][self::$uid]) ){
                    $this->_cache[self::$id][self::$uid] = $time;
                    $this->_setCache(true);
                    return 1;
        	}
        	
        	if(self::$lefttime <= $time-$this->_cache['endTime']){
        		    //清空缓存
	                $key= "browse:".$this->key ;
	                $this->_cache['endTime'] = $time;
	                $result = $this->_cache[self::$id];
	                unset($this->_cache[self::$id]);
	                $this->_setCache(true);
	                return $result;
        	}
        }

        /**
         * _statistics 
         * 统计
         * @param mixed $id 
         * @param mixed $uid 
         * @access private
         * @return void
         */
        private function _statistics(){
            //检查时间
            $count = 0;
            foreach( $this->_cache as $value ){
                if( $value['id'] == self::$id)
                    $count++;
            }
            return $count;
        }

        /**
         * _setCache 
         * 设置缓存
         * @param mixed $id 
         * @param mixed $uid 
         * @access private
         * @return void
         */
        private function _setCache($replace){
            $this->_old_cache[self::$type] = $this->_cache;
            $result = $this->_cacheMode->put( 'browse:'.$this->key,$this->_old_cache,$replace );
			return $result;
        }

        /**
         * setOptions 
         * 批量设置
         * @param array $options 
         * @access public
         * @return void
         */
        public function setOptions(array $options ){
            $option = array_change_key_case( $options,CASE_LOWER );

            //循环设置属性
            foreach ( $option as $key => $value ){
            	if($key === "key"){
            		$this->key = $value;
            		continue;
            	}
                self::$$key = $value;
            }
            return $this;
        }
    }
